package com.weetop.cfw.base.presenter.imp

import com.weetop.cfw.base.presenter.FeedBackPresenter
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.FeedBackView
import com.weetop.cfw.bean.ErrorBean
import com.weetop.cfw.bean.UploadImageBean
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.mine.activity.FeedBackActivity
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class FeedBackPresenterImp(var feedBackView: FeedBackView? = null):FeedBackPresenter {


    /**
     * 意见反馈
     */
    override fun feedback(feedBackActivity: FeedBackActivity, content: String, imageUrlList: String) {
        when {
            content.isEmpty() -> {
                feedBackActivity.showNativeShortToast("请输入反馈内容")
            }
            content.length < 15 -> {
                feedBackActivity.showNativeShortToast("反馈内容最少输入15字")
            }
            else -> {
                feedBackActivity.showLoadingDialog(feedBackActivity,"正在反馈...")
                RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().feedback(content,imageUrlList),feedBackActivity,object:RxJavaCallBack<ErrorBean>(){
                    override fun onSuccess(t: ErrorBean?) {
                        feedBackActivity.dismissLoadingDialog()
                        feedBackView?.feedbackSuccess()
                    }

                    override fun onRequestError(t: ErrorBean?) {
                        super.onRequestError(t)
                        feedBackActivity.dismissLoadingDialog()
                    }

                    override fun onErrorResponse(throwable: Throwable?) {
                        super.onErrorResponse(throwable)
                        feedBackActivity.dismissLoadingDialog()
                    }
                })
            }
        }
    }

    /**
     * 上传图片
     */
    override fun uploadImage(feedBackActivity: FeedBackActivity, feedbackFile: String) {
        feedBackActivity.showLoadingDialog(feedBackActivity,"正在上传图片...")
        val file = File(feedbackFile)
        val image: RequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file)
        val feedbackImgPart = MultipartBody.Part.createFormData("Imgfile", file.name, image)
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().uploadImage(multipartFile = feedbackImgPart),feedBackActivity,object:RxJavaCallBack<UploadImageBean>(){

            override fun onSuccess(t: UploadImageBean?) {
                feedBackActivity.dismissLoadingDialog()
                feedBackView?.uploadImageSeccess(t)
            }

            override fun onRequestError(t: UploadImageBean?) {
                super.onRequestError(t)
                feedBackActivity.dismissLoadingDialog()
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                feedBackActivity.dismissLoadingDialog()
            }

        })
    }


    /**
     * 绑定
     */
    override fun attachView(baseView: BaseView) {
        feedBackView = baseView as? FeedBackView
    }

    /**
     * 解绑
     */
    override fun detachView() {
        feedBackView = null
    }
}